define([
    'text!templates/diary/table.html',
    'collections/DiaryList',
    'views/DiaryTableRowView',
    'jquery',
    'underscore',
    'backbone'
], function(template, DiaryList, DiaryTableRowView) {

    var DiaryTableView = Backbone.View.extend({
        el: $('#content'),
        page: 0,
        initialize: function() {

        },
        render: function() {
            console.log("DiaryTableView.render()");
            var element = this.$el;
            var diaryList = new DiaryList();

            var actualPageIdx = this.page;
            if (actualPageIdx < 0) {
                actualPageIdx = 0;
            }
            diaryList.fetch({
                url: "service/diaries/" + actualPageIdx,
                success: function() {
                    var compiledTemplate = _.template(template,
                            {
                                rows: generateRows(diaryList),
                                actualPage: actualPageIdx
                            });
                    element.html(compiledTemplate);
                }
            });
        }
    });

    var generateRows = function(diaryList) {
        var rows = new Array();
        var models = diaryList.models;
        for (var i = 0; i < models.length; i++)
        {
            var diary = models[i];
            var rowView = new DiaryTableRowView();
            rowView.model = diary;
            rowView.render();
            rows[i] = rowView.$el.html();
        }
        return  rows;
    };

    return DiaryTableView;
});


